package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.cart.vo.AddCartSuccessVo;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.cart.CartFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
public class CartController {

    @Autowired
    private CartFeignClient cartFeignClient ;

//    @Autowired
//    private HttpServletRequest httpServletRequest ;

//    public static final ThreadLocal<HttpServletRequest> HTTP_SERVLET_REQUEST_THREAD_LOCAL = new ThreadLocal<>() ;

    @GetMapping(value = "/addCart.html")
    public String addCart(@RequestParam(value = "skuId") Long skuId , @RequestParam(value = "skuNum") Integer skuNum , Model model){

//        HTTP_SERVLET_REQUEST_THREAD_LOCAL.set(httpServletRequest);

        // 远程调用service-cart微服务的添加商品到购物车的接口
        log.info("addCart方法执行了...skuId:{} , skuNum: {} , userId: {} , userTempId: {}" , skuId , skuNum);
        Result<AddCartSuccessVo> addCart = cartFeignClient.addCart(skuId, skuNum);
        AddCartSuccessVo addCartSuccessVo = addCart.getData();
        if(addCartSuccessVo == null) {
            return "cart/error" ;
        }

        // 把响应数据存储到Model数据模型中
        model.addAttribute("skuInfo" , addCartSuccessVo.getSkuInfo()) ;
        model.addAttribute("skuNum" , addCartSuccessVo.getSkuNum()) ;

        // 返回
        return "cart/addCart" ;

    }

    @GetMapping(value = "/cart.html")
    public String cartList(){
        return "cart/index" ;

    }

    @GetMapping(value = "/cart/deleteChecked")
    public String deleteChecked() {
        cartFeignClient.deleteCheckedCart() ;
        return "cart/index" ;
    }

}
